using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;

namespace Acms.SqlDal
{
    public class UserDal : Base
    {
        public UserDal() : base() { }

        public enum Fields
        {
            Id,
            SiteId,
            UserName,
            FirstName,
            LastName,
            DateOfBirth,
            EmailAddress,
            Gender,
            Address1,
            Address2,
            Town,
            PostCode,
            MobileTel,
            Created
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.SiteId, reader, table);
            GetReaderValue<string>(Fields.UserName, reader, table);
            GetReaderValue<string>(Fields.FirstName, reader, table);
            GetReaderValue<string>(Fields.LastName, reader, table);
            GetReaderValue<DateTime>(Fields.DateOfBirth, reader, table);
            GetReaderValue<string>(Fields.EmailAddress, reader, table);
            GetReaderValue<byte>(Fields.Gender, reader, table);
            GetReaderValue<string>(Fields.Address1, reader, table);
            GetReaderValue<string>(Fields.Address2, reader, table);
            GetReaderValue<string>(Fields.Town, reader, table);
            GetReaderValue<string>(Fields.PostCode, reader, table);
            GetReaderValue<string>(Fields.MobileTel, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
        }

        public System.Collections.Hashtable[] GetAll()
        {
            SqlCommand cmd = CreateCommand("GetAll");
            return ExecuteGetCollection(cmd);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByUserName(string userName, int siteId)
        {
            SqlCommand cmd = CreateCommand("GetByUserName");
            AddInputParameter(cmd, "UserName", userName, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByEmailAddress(string emailAddress, int siteId)
        {
            SqlCommand cmd = CreateCommand("GetByEmailAddress");
            AddInputParameter(cmd, "EmailAddress", emailAddress, SqlDbType.VarChar, 250);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetFromLoginKey(Guid key, DateTime lastAccessed)
        {
            SqlCommand cmd = CreateCommand("GetFromLoginKey");
            AddInputParameter(cmd, "Key", key, SqlDbType.UniqueIdentifier);
            AddInputParameter(cmd, "LastAccessed", lastAccessed, SqlDbType.DateTime);
            return ExecuteGetItem(cmd);
        }

        public void DeleteLoginKey(Guid key, int id)
        {
            SqlCommand cmd = CreateCommand("DeleteLoginKey");
            AddInputParameter(cmd, "Key", key, SqlDbType.UniqueIdentifier);
            AddInputParameter(cmd, "UserId", id, SqlDbType.Int);
            ExecuteCommand(cmd);
        }

        public Hashtable[] Search(int siteId)
        {
            SqlCommand cmd = CreateCommand("Search");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }
    }
}
